package com.gitee.feizns.v8.utils;

import lombok.SneakyThrows;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;

/**
 * @author feizns
 * @since 2019/8/13
 */
public abstract class IOUtils {

    @SneakyThrows
    public static String readerToStr(InputStream is) {
        InputStreamReader reader = new InputStreamReader(is, "UTF-8");
        StringWriter sw = new StringWriter();

        char[] tmp = new char[1024];
        int len = -1;
        while ((len = reader.read(tmp)) != -1 ) {
            sw.write(tmp, 0, len);
        }
        return sw.toString();
    }

    @SneakyThrows
    public static void write(String str, File file) {
        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(str.getBytes());
            fos.flush();
        }
    }

}
